package com.xuanxuan.socketdemo.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xuanxuan.socketdemo.R;

import java.util.ArrayList;

/**
 * author : zhouxuan77
 * e-mail : zhouxuan77@163.com
 * time   : 2022/03/17
 * desc   :
 * version: 1.0
 */
public class TerminalAdapter extends RecyclerView.Adapter<TerminalAdapter.ViewHolder> {

    private ArrayList<String> data;

    public TerminalAdapter(ArrayList<String> data) {
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_row_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.getTv_head().setText(String.valueOf(position));
        holder.getTv_text().setText(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addText(String string) {
        data.add(getItemCount(), string);

        notifyItemInserted(getItemCount() - 1);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_head;
        private final TextView tv_text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tv_head = itemView.findViewById(R.id.tv_row_item_head);
            tv_text = itemView.findViewById(R.id.tv_row_item_text);
        }

        public TextView getTv_head() {
            return tv_head;
        }

        public TextView getTv_text() {
            return tv_text;
        }
    }
}
